let instance;

/**
 * 统一的音效管理器
 */
export default class Music {
  bgmAudio = wx.createInnerAudioContext();
  clickAudio = wx.createInnerAudioContext();
  moveAudio = wx.createInnerAudioContext();
  winAudio = wx.createInnerAudioContext();
  loseAudio = wx.createInnerAudioContext();
  status = wx.getStorageSync("music") || "on";
  constructor() {
    if (instance) return instance;

    instance = this;

   
    // 设置音量，例如设置为0.5（50%音量）
    // this.bgmAudio.volume = 0.1;
    this.bgmAudio.src = "audio/bg.mp3";
    this.clickAudio.src = "audio/click.mp3";
    this.moveAudio.src = "audio/move.mp3";
    this.winAudio.src = "audio/win.mp3";
    this.loseAudio.src = "audio/lose.mp3";
    console.log(this.status === "pause");
    this.bgmAudio.loop = true; // 背景音乐循环播放
     if (this.status === "pause") {
       this.stopAll();
     }else{
      this.bgmAudio.autoplay = true; // 背景音乐自动播放
    }
    
  }

  playClick() {
    this.clickAudio.currentTime = 0;
    this.status === "on" && this.clickAudio.play();
  }

  playMove() {
    this.moveAudio.currentTime = 0;
    this.status === "on" && this.moveAudio.play();
  }

  playWin() {
    this.winAudio.currentTime = 0;
    this.status === "on" && this.winAudio.play();
  }

  playLose() {
    this.loseAudio.currentTime = 0;
    this.status === "on" && this.loseAudio.play();
  }

  stopAll() {
    this.bgmAudio.pause();
    this.status = "pause";
    wx.setStorageSync("music", "pause");
    // this.clickAudio.stop();
  }
  playAll() {
    this.bgmAudio.play();
    this.status = "on";
    wx.setStorageSync("music", "on");
  }
}
